package homework_2;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

public class enlarge {
	public static BufferedImage image;

    public static void Read(String filename) throws IOException {
        File f = new File(filename);
        image = ImageIO.read(f);
    }
    public static void main(String args[]){
    	try {
			Read("src/123.jpg");
    		int width = image.getWidth();        // 源图宽   
            int height = image.getHeight();        // 源图高  
			int [] srcPixels = new int[height*width];
			//把原图rgb值存入一位数组
	    	for(int row=0, i=0;row <height ;row++ ){
	    		for(int col = 0;col < width;i++,col++){
 	    			srcPixels[i]=image.getRGB(col,row);
	    		}
	    	}
	    	
	    	BufferedImage a = new BufferedImage(3200,2000,BufferedImage.TYPE_INT_RGB);
	    	//把原图rgb值拆开插值后合并存入一位数组
	    	int [] output = zoomIn(srcPixels,width,height,3200,2000);
	    	//画图
	    	for(int row=0, i=0;row <2000;row++ ){
	    		for(int col = 0;col < 3200;i++,col++){
	    			a.setRGB(col,row,output[i]);
	    		}
	    	}
	    	//输出文件至src文件夹
	    	ImageIO.write(a,"jpg",new File("src/123enlarge.jpg"));
		} catch (IOException e) {
			e.printStackTrace();
		}
    	
    }

    public static int[] zoomIn(int[] inPixelsData, int srcW, int srcH, int destW, int destH) {
        double[][][] input3DData = ResolvePixels(inPixelsData, srcH, srcW);

        int[][][] outputThreeDeminsionData = new int[destH][destW][4];
        float rowRatio = ((float)srcH) / ((float)destH);
        float colRatio = ((float)srcW) / ((float)destW);

        for(int row = 0; row < destH; row++) {

            double srcRow = ((float)row) * rowRatio;
            double j = Math.floor(srcRow);
            double t = srcRow - j;

            for(int col = 0; col < destW; col++) {

                double srcCol = ((float)col) * colRatio;
                double k = Math.floor(srcCol);
                double u = srcCol - k;

                double coffiecent1 = (1.0d - t) * (1.0d - u);
                double coffiecent2 = (t) * (1.0d - u);
                double coffiecent3 = t * u;
                double coffiecent4 = (1.0d - t) * u;

                for (int i = 0; i < 4; i++) {
                    outputThreeDeminsionData[row][col][i] = (int)(
                        coffiecent1 * input3DData[getClip((int)j, srcH - 1, 0)][getClip((int)k, srcW - 1, 0)][i] +
                        coffiecent2 * input3DData[getClip((int)(j + 1), srcH - 1, 0)][getClip((int)k, srcW - 1, 0)][i] +
                        coffiecent3 * input3DData[getClip((int)(j + 1), srcH - 1, 0)][getClip((int)(k + 1),srcW - 1, 0)][i] +
                        coffiecent4 * input3DData[getClip((int)j, srcH - 1, 0)][getClip((int)(k + 1), srcW - 1, 0)][i]
                    );

                }
            }
        }
  
        return ComposePixels(outputThreeDeminsionData, destW, destH);
    }

    private static int getClip(int x, int max, int min) {
        return x > max ? max : x < min ? min : x;
    }

    		//将alpha，rgb四个值合成为PIXEL值 存在一位数组中
    public static int[] ComposePixels(int[][][] data, int imgCols, int imgRows) {
        int[] oneDPix = new int[imgCols * imgRows * 4];				

        for (int row = 0, cnt = 0; row < imgRows; row++) {
            for (int col = 0; col < imgCols; col++) {

                oneDPix[cnt] = 
                          ((data[row][col][0] << 24) & 0xFF000000)
                        | ((data[row][col][1] << 16) & 0x00FF0000)
                        | ((data[row][col][2] << 8) & 0x0000FF00)
                        | ((data[row][col][3]) & 0x000000FF);

                cnt++;
            }
        }
        return oneDPix;    //返回ARGB值
    }

    		//将PIXELS值分解成alpha，red，green，blue值存在三维数组中
    public static double [][][] ResolvePixels(int[] oneDPix2, int imgRows, int imgCols) {
        double[][][] tempData = new double[imgRows][imgCols][4];
        for(int row=0; row<imgRows; row++) {

            int[] aRow = new int[imgCols];
            for (int col = 0; col < imgCols; col++) {
                int element = row * imgCols + col;
                aRow[col] = oneDPix2[element];
            }

            for(int col = 0; col < imgCols; col++) {
                tempData[row][col][0] = (aRow[col] >> 24) & 0xFF; // alpha
                tempData[row][col][1] = (aRow[col] >> 16) & 0xFF; // red
                tempData[row][col][2] = (aRow[col] >> 8) & 0xFF;  // green
                tempData[row][col][3] = (aRow[col]) & 0xFF;       // blue
            }

        }
        return tempData;
    }
}
